﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// Summary description for GetChucNang
/// </summary>
public class ChucNangDA
{
    Connection conn = new Connection();
    
    public List<ChucNang> GetData(int loaiid,int nhanhieuid)
    {
        try
        {
            List<ChucNang> lstChucNang = new List<ChucNang>();
            conn.openCon();
            string sql = " SELECT * FROM ChucNang where ID in(select ChucNangID from sanpham where sanpham.loaiid=@idloai and sanpham.nhanhieuid=@idnhanhieu group by ChucNangID ) and Xoa=0";
            SqlCommand cmd = new SqlCommand(sql, conn.getCon());
            cmd.Parameters.Add("@idloai", System.Data.SqlDbType.Int).Value = loaiid;
            cmd.Parameters.Add("@idnhanhieu", System.Data.SqlDbType.Int).Value = nhanhieuid;
            SqlDataReader reader = cmd.ExecuteReader();
            ChucNang objChucNang = null;
            while (reader.Read())
            {
                objChucNang = new ChucNang(reader.GetInt32(0), reader.GetString(1), reader.GetBoolean(3));
                lstChucNang.Add(objChucNang);
            }
            reader.Close();
            conn.closeCon();
            return lstChucNang;
        }
        catch (Exception)
        {

            throw;
        }
        finally {
            conn.closeCon();
        }
    }
    public ChucNang getByID(int id)
    {
        try
        {
            string str = "select * from ChucNang where id =@id";
            SqlCommand com = new SqlCommand(str, conn.getCon());
            com.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
            conn.openCon();
            SqlDataReader dr = com.ExecuteReader();
            ChucNang cn = new ChucNang();
            if (dr.Read())
            {
                cn = new ChucNang(dr.GetInt32(0), dr.GetString(1), dr.GetBoolean(3));
            }
            conn.closeCon();
            return cn;
        }
        catch (Exception)
        {

            throw;
        }
        finally {
            conn.closeCon();
        }
        
    }
    public void xoaByID(int id)
    {
        try
        {
            string str = "update ChucNang set Xoa=1 where ID=@ID";
            SqlCommand com = new SqlCommand(str, conn.getCon());
            com.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            conn.openCon();
            com.ExecuteNonQuery();
            conn.closeCon();
        }
        catch (Exception ex)
        {

            throw new Exception("Lỗi ở chucnangDA" + ex.Message);
        }
        finally
        {
            conn.closeCon();
        }
    }
    
}